package com.woniuxy.controller;

import com.woniuxy.dao.CommentRepo;
import com.woniuxy.entity.*;
import com.woniuxy.service.CampService;
import com.woniuxy.service.FoodService;
import com.woniuxy.util.Page;
import com.woniuxy.util.QiniuCloudUtil;
import com.woniuxy.util.Result;
import com.woniuxy.vo.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class FoodController {
    @Resource
    private FoodService foodServiceImpl;
    @Resource
    private CommentRepo commentRepo;

    /**
     * 美食管理的菜品的查询展示
     *
     * @param index
     * @param number
     * @param foodName
     * @param address
     * @param minPrice
     * @param maxPrice
     * @return
     * @throws Exception
     */
    @RequestMapping("/selectFoodExample")
    public Result selectFoodExample(Integer index, Integer number, String foodName, String address, Integer minPrice, Integer maxPrice) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("index", index);
        map.put("number", number);
        map.put("foodName", foodName);
        map.put("address", address);
        map.put("minPrice", minPrice);
        map.put("maxPrice", maxPrice);
        System.out.println(map);
        List<FoodVO> list = foodServiceImpl.pageSelectFoodByExample(map);
        int dataCount = foodServiceImpl.countFoodByExample(map);
        Page page = new Page();
        int pageCount = dataCount % number == 0 ? dataCount / number : dataCount / number + 1;
        page.setPageIndex(index);
        page.setNumber(number);
        page.setCount(dataCount);
        page.setPageCount(pageCount);
        System.out.println(list);
        System.out.println(page);
        return new Result("200", "success", page, list);
    }

    /**
     * 美食管理的菜品的信息的更新
     *
     * @param foodVOo
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateFood")
    public Result updateFood(FoodVO foodVOo) throws Exception {
        Food food = new Food();
        food.setFoodId(foodVOo.getFoodId());
        food.setAddress(foodVOo.getAddress());
        food.setDescribe(foodVOo.getDescribe());
        food.setEndTime(foodVOo.getEndTime());
        System.out.println(foodVOo.getEndTime());
        food.setFoodName(foodVOo.getFoodName());
        food.setPrice(foodVOo.getPrice());
        food.setStartTime(foodVOo.getStartTime());
        System.out.println(foodVOo.getStartTime());
        food.setStock(foodVOo.getStock());
        food.setTel(foodVOo.getTel());
        int i = foodServiceImpl.updateFood(food);
        return new Result("200", "success", null, null);
    }

    /**
     * 美食管理的菜品的信息的删除
     *
     * @param foodId
     * @return
     * @throws Exception
     */
    @RequestMapping("/deleteFood")
    public Result deleteFood(Integer foodId) throws Exception {
        int res = foodServiceImpl.deleteFood(foodId);
        return new Result("200", "success", null, null);
    }

    /**
     * 美食订单的查询
     * @param index
     * @param number
     * @param cityName
     * @param campname
     * @param foodName
     * @param minPrice
     * @param maxPrice
     * @return
     * @throws Exception
     */
    @RequestMapping("/selectFoodOrder")
    public Result selectFoodOrder(Integer index,Integer number,String cityName,String campname,String foodName,Integer minPrice,Integer maxPrice)throws Exception{
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("index",index);
        map.put("number",number);
        map.put("cityName",cityName);
        map.put("campname",campname);
        map.put("foodName",foodName);
        map.put("minPrice",minPrice);
        map.put("maxPrice",maxPrice);
        List<FoodOrderVO> list = foodServiceImpl.selectFoodOrderByExample(map);
        int dataCount = foodServiceImpl.countFoodOrder(map);
        int pageCount = dataCount%number==0?dataCount/number:dataCount/number+1;
        Page page = new Page();
        page.setNumber(number);
        page.setCount(dataCount);
        page.setPageCount(pageCount);
        page.setPageIndex(index);
        System.out.println(list);
        return new Result("200","success",page,list);
    }

    /**
     * 上传图片
     * @param file
     * @return
     */
    @RequestMapping("/uploadFoodImgs")
    public String uploadFoodImgs(MultipartFile file){
        MultipartFile avatar=file;
        System.out.println("参数："+avatar);
        String fileName = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)+"_"+avatar.getOriginalFilename();
        try {
            System.out.println("文件名："+fileName);
            QiniuCloudUtil.upload(fileName,avatar.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        //返回最终存入数据库的地址
        return QiniuCloudUtil.fullPath(fileName);
    }

    /**
     * 添加美食返回主键
     * @param food
     * @return
     */
    @RequestMapping("/addFood")
    public Result addFood(Food food) throws Exception {
        int res=foodServiceImpl.insertFoodReturnFoodId(food);
        Integer foodId = food.getFoodId();
        int campId=food.getCampId();
        int resAddress=foodServiceImpl.insertFoodAddress(foodId,campId);
        return new Result("200","success",foodId,null);
    }

    /**
     * 在数据库中新增图片地址
     * @param foodId
     * @param url
     * @return
     */
    @RequestMapping("/addFoodImg")
    public Result addFoodImg(int foodId,String url) throws Exception {
        int i = foodServiceImpl.insertFoodImg(foodId, url);
        return new Result("200","success",i,null);
    }

    /**
     * 在新增美食中通过选择城市选择营地
     * @param cityId
     * @return
     * @throws Exception
     */
    @RequestMapping("/selectCampByCityName")
    public Result selectCampByCityName(int cityId) throws Exception {
        List<Camp> list = foodServiceImpl.selectCampByCityName(cityId);
        return new Result("200","success",null,list);
    }

    /**
     * 利用elasticsearch搜索评论的相关内容
     * @param index
     * @param number
     * @param foodName
     * @param describe
     * @param comment
     * @return
     * @throws Exception
     */
    @RequestMapping("/selectComment")
    public Result selectComment(Integer index, Integer number, String foodName, String describe,String  comment) throws Exception {
        List<FoodCommentlVO>  FoodCommentList=new ArrayList<FoodCommentlVO>();
        if ("".equals(foodName) && "".equals(describe) && "".equals(comment)){
            //FoodCommentList = commentRepo.selectAllFoodComment();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("index", index);
            map.put("number", number);
            map.put("foodName", foodName);
            map.put("describe", describe);
            map.put("comment", comment);
            List<FoodCommentlVO> list = foodServiceImpl.selectFoodCommentByExample(map);
            int dataCount = foodServiceImpl.countFoodCommentByExample(map);
            System.out.println(dataCount);
            int pageCount = dataCount % number == 0 ? dataCount / number : dataCount / number + 1;
            Page page = new Page(index, dataCount, pageCount, number);
            return new Result("200", "success", page, list);
        }
          FoodCommentList = commentRepo.selectFoodCommentByExampleES(foodName,describe,comment);
        //查询出来的总条数
        int dataCount = FoodCommentList.size();
        //计算总共多少页
        int pageCount = dataCount % number == 0 ? dataCount / number : dataCount / number + 1;
        Page page = new Page(index, dataCount, pageCount, number);
        //往前端传的每页数据
        List<FoodCommentlVO> list = new ArrayList<>();
        //在FoodCommentList中挑选每页查询的数据放进list中
        int begin = (index-1)*number;
        for(int i=begin;i<begin+number;i++){
            if (i>=FoodCommentList.size()-1){
                list.add(FoodCommentList.get(FoodCommentList.size()-1));
                break;
            }else {
                list.add(FoodCommentList.get(i));
            }
        }

        if (FoodCommentList.size()==0){
            return new Result("200", "success", page, list);
        }else{
            return new Result("200", "success", page, list);
        }


    }


}
